package mystack;

import java.util.Arrays;
import java.util.Stack;

public class MyStack {
    private int usedSize;
    private int[] elem;
    private final int DEFAULT_CAPACITY = 10;

    public MyStack(){
        elem = new int[DEFAULT_CAPACITY];
    }

    public int push(int val){
        if (isFull()) {
            elem = Arrays.copyOf(elem,2 * elem.length);
        }
        elem[usedSize++] = val;
        return val;
    }

    private boolean isFull(){
        return usedSize == elem.length;
    }

    public int pop() {
        if (isEmpty()){
            throw new EmptyException("栈为空...");
        }
        usedSize--;
        return elem[usedSize];
    }

    public boolean isEmpty(){
        return usedSize == 0;
    }

    public int peek(){
        if (isEmpty()) {
            throw new EmptyException("栈为空...");
        }
        return elem[usedSize-1];
    }

}
